using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;
using RADB;
using System.IO;


namespace Kitchen
{
    public partial class Form1 : XtraForm
    {
        IDataTransfer DTO;
        Dictionary<int, string> productName = new Dictionary<int, string>();
        public Form1()
        {
            InitializeComponent();
            InitGrid();
        }
        void InitGrid()
        {
            //BindingList<Person> gridDataList = new BindingList<Person>();
            //gridDataList.Add(new Person("Joth", "Smith"));
            //gridDataList.Add(new Person("Gabriel", "Smith"));
            //gridDataList.Add(new Person("Ashley", "Smith", "some comment"));
            //gridDataList.Add(new Person("Adrian", "Smith", "some comment"));
            //gridDataList.Add(new Person("Gabriella", "Smith", "some comment"));
            //gridControl.DataSource = gridDataList;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            IDataTransferFactory.RegisterChannel();
            DTO = IDataTransferFactory.getDataTransferObject(Properties.Settings.Default.RemoteObjAddress);

            lvItems.Groups.Add("1", "Waiting");
            lvItems.Groups.Add("2", "Cooking");


            DataTable products = DTO.getProducts();

            imgProducts.Images.Clear();            
            foreach (DataRow row in products.Rows)
            {
                if (row["Image"] != DBNull.Value)
                {
                    MemoryStream mstream = new MemoryStream((Byte[])row["Image"]);
                    Image img = Image.FromStream(mstream);
                    imgProducts.Images.Add(row["ID"].ToString(), img);                    
                }

                productName.Add((int)row["ID"], (string)row["Name"]);
            }
            DataTable waitingItems = DTO.getWaitingItems();
            DataTable cookingItems = DTO.getCookingItems();

            foreach (DataRow row in waitingItems.Rows)
            {
                ListViewItem item = lvItems.Items.Add(row["ID"].ToString(), string.Format("{1}\n({0})\n{2}", productName[(int)row["Product"]], row["Quantity"].ToString(), ((DateTime)row["PlacingTime"]).ToShortTimeString()), row["product"].ToString());
                item.Group = lvItems.Groups["1"];
            }

            foreach (DataRow row in cookingItems.Rows)
            {
                ListViewItem item = lvItems.Items.Add(row["ID"].ToString(), string.Format("{1}\n({0})\n{2}", productName[(int)row["Product"]], row["Quantity"].ToString(), ((DateTime)row["PlacingTime"]).ToShortTimeString()), row["product"].ToString());
                item.Group = lvItems.Groups["2"];
            }
            
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DataTable waitingItems = DTO.getWaitingItems();
            DataTable cookingItems = DTO.getCookingItems();

            List<int> items = new List<int>();
            foreach (DataRow row in waitingItems.Rows)
            {
                ListViewItem item = lvItems.Items[row["ID"].ToString()];
                if (item == null)
                {
                    
                    item = lvItems.Items.Add(row["ID"].ToString(), string.Format("{1}\n({0})\n{2}", productName[(int)row["Product"]], row["Quantity"].ToString(), ((DateTime)row["PlacingTime"]).ToShortTimeString()), row["product"].ToString());
                    item.Group = lvItems.Groups["1"];
                }
                else
                {
                    if (item.Group.Name != "1")
                    {
                        item.Group = lvItems.Groups["1"]; 
                    }                    
                }
                items.Add((int)row["ID"]);
            }

            foreach (DataRow row in cookingItems.Rows)
            {
                ListViewItem item = lvItems.Items[row["ID"].ToString()];
                if (item == null)
                {

                    item = lvItems.Items.Add(row["ID"].ToString(), string.Format("{1}\n({0})\n{2}", productName[(int)row["Product"]], row["Quantity"].ToString(), ((DateTime)row["PlacingTime"]).ToShortTimeString()), row["product"].ToString());
                    item.Group = lvItems.Groups["1"];
                }
                else
                {
                    if (item.Group.Name != "2")
                    {
                        item.Group = lvItems.Groups["2"];
                    }
                }
                items.Add((int)row["ID"]);
            }

            List<ListViewItem> removeItem = new List<ListViewItem>();
            foreach (ListViewItem item in lvItems.Items)
            {
                if (items.IndexOf(int.Parse(item.Name)) <0)
                {
                    removeItem.Add(item);
                }
            }
            foreach (ListViewItem item in removeItem)
            {
                lvItems.Items.Remove(item);
            }
        }

        private void lvItems_ItemActivate(object sender, EventArgs e)
        {
            if (lvItems.SelectedItems.Count ==0)
            {
                return;
            }
            ListViewItem item = lvItems.SelectedItems[0];
            if (item.Group.Name == "1")
            {
                if (DTO.setItemCooking(1, int.Parse(item.Name)))
                {
                    item.Group = lvItems.Groups["2"];
                }
                else
                {
                }
            }
            else
            {
                if (DTO.notifyAnItem(int.Parse(item.Name)))
                {
                    lvItems.Items.Remove(item);
                }
                else
                {
                }
            }
        }

    }
}